package com.qf.controller;

import com.qf.service.AdminService;
import com.qf.service.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.PasswordAuthentication;

/**
 * @author 千锋健哥
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 设置请求对象的字符集
        req.setCharacterEncoding("utf-8");
        //2. 获取请求对象中的参数
        //获取用户名
        String username = req.getParameter("username");
        //获取密码
        String pwd = req.getParameter("pwd");

        //获取cookie对象
        Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            //遍历cookie对象数组
            for (Cookie cookie : cookies) {
                if ("token".equals(cookie.getName())) {
                    resp.getWriter().println("<html>");
                    resp.getWriter().println("<head>");
                    resp.getWriter().println("<title>用户已登录过</title>");
                    resp.getWriter().println("</head>");
                    resp.getWriter().println("<body>");
                    resp.getWriter().println("<h1>该用户已经登录过, 用户名为: " +cookie.getValue()+ "</h1>");
                    resp.getWriter().println("</body>");
                    resp.getWriter().println("</html>");
                    return;
                }
            }
        }

        //3. 调用service执行用户名密码校验业务
        AdminService adminService = new AdminServiceImpl();
        boolean flag = adminService.login(username, pwd);

        //4. 跳转到用户列表Servlet
        //请求转发跳转, 将请求以及响应对象传到跳转后的servlet中, 路径写跳转的目标路径
        //req.getRequestDispatcher("/AdminList").forward(req, resp);
        //重定向跳转
        //resp.sendRedirect("/AdminList");
        //resp.sendRedirect("http://www.baidu.com");

        //4. 根据service返回结果, 将数据返回给页面
        //设置响应字符集编码
        resp.setContentType("text/html; charset=utf-8");

        //创建新的cookie对象
        Cookie cookie = new Cookie("token", username);
        //cookie的访问路径
        cookie.setPath("/");
        //可以访问这个cookie的网站的域名
        cookie.setDomain("localhost");
        //cookie的生存时间, 浏览器关闭后, 这个cookie自动销毁
        cookie.setMaxAge(-1);
        //将cookie对象放入响应对象中, 写到用户浏览器中保存
        resp.addCookie(cookie);

        if (flag) {
            //登录成功
            resp.getWriter().println("<html>");
            resp.getWriter().println("<head>");
            resp.getWriter().println("<title>登录成功</title>");
            resp.getWriter().println("</head>");
            resp.getWriter().println("<body>");
            resp.getWriter().println("<h1>登录成功</h1>");
            resp.getWriter().println("</body>");
            resp.getWriter().println("</html>");
        } else {
            //登录失败
            resp.getWriter().println("<html>");
            resp.getWriter().println("<head>");
            resp.getWriter().println("<title>登录失败</title>");
            resp.getWriter().println("</head>");
            resp.getWriter().println("<body>");
            resp.getWriter().println("<h1>登录失败, 用户名或者密码错误!</h1>");
            resp.getWriter().println("</body>");
            resp.getWriter().println("</html>");
        }
    }

}
